package service

import (
	"fmt"
	"go-demo/common"
	"go-demo/dao"
	"go-demo/model"
)

func ExecRelationAction(token string, toUserId int64, actionType string) interface{} {
	//验证签名是否过期
	parseToken, err := common.ParseToken(token)
	if parseToken == nil && err != nil {
		return nil
	}
	user := common.UsersLoginInfo[token]

	var follow model.FollowEvent
	//关注人id
	follow.FollowerId = user.Id
	//被关注人id
	follow.FollowId = toUserId
	//根据动作类型判断
	if actionType == "1" {
		//添加关注记录
		result := dao.AddFollow(follow)
		fmt.Print(result)
	} else if actionType == "2" {
		//删除关注记录
		result := dao.DeleteFollow(follow)
		fmt.Print(result)
	}
	return common.SuccessCode
}

func GetFollowList(token string, userId int64) interface{} {
	//验证签名是否过期
	parseToken, err := common.ParseToken(token)
	if parseToken == nil && err != nil {
		return nil
	}

	follows := dao.GetFollowList(userId)
	var users []model.User
	for _, follow := range follows {
		user, _ := dao.GetUserByID(follow.FollowId)
		users = append(users, user)
	}
	//改为已关注状态
	for key := range users {
		users[key].IsFollow = true
	}
	return users
}
func GetFollowerList(token string, userId int64) interface{} {
	//验证签名是否过期
	parseToken, err := common.ParseToken(token)
	if parseToken == nil && err != nil {
		return nil
	}
	//获取关注列表
	follows := GetFollowList(token, userId)
	//获取粉丝列表
	followers := dao.GetFollowerList(userId)

	var users []model.User
	//找到关注列表和粉丝列表重叠的用户,并将状态改为已关注
	for _, follower := range followers {
		user, _ := dao.GetUserByID(follower.FollowerId)
		for _, follow := range follows.([]model.User) {
			if user.Id == follow.Id {
				user.IsFollow = true
			}
		}
		users = append(users, user)
	}
	return users
}

func GetFriendList(token string, userId int64) interface{} {
	//验证签名是否过期
	parseToken, err := common.ParseToken(token)
	if parseToken == nil && err != nil {
		return nil
	}
	var users []model.User
	followUsers := GetFollowList(token, userId)
	followerUsers := GetFollowerList(token, userId)
	//找到关注列表和粉丝列表重叠的用户
	for _, followUsers := range followUsers.([]model.User) {
		for _, followerUsers := range followerUsers.([]model.User) {
			if followUsers.Id == followerUsers.Id {
				users = append(users, followUsers)
			}
		}
	}
	return users
}
